package it.unica.informatica.enotes.notes;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;


/**
 * This class manages a generic attachment contained inside a note
 *
 * @author Mirko Marras
 * @version 1.0
 */
public class AttachmentItem implements Parcelable {

   public static final String TYPE_IMAGE = "image/*";
   public static final String TYPE_VIDEO = "video/*";
   public static final String TYPE_AUDIO = "audio/*";

   public static final int STATE_VALID = 0;
   public static final int STATE_EXTENSION_ERROR = 1;
   public static final int STATE_SIZE_ERROR = 2;
   public static final int STATE_DURATION_ERROR = 3;

   private String name;
   private String type;
   private Bitmap thumbnails;
   private int state;

   /**
    * Construct a new attachment with default values
    */
   public AttachmentItem() {
      this.name = "";
      this.type = "";
      this.thumbnails = null;
      this.state = STATE_VALID;
   }

   /**
    * Construct a new attachment based on the values passed as parameters
    *
    * @param name       The name of the attachment
    * @param type       The type of the attachment
    * @param thumbnails The image used to show a preview of the attachment
    */
   public AttachmentItem(String name, String type, Bitmap thumbnails) {
      this.name = name;
      this.type = type;
      this.thumbnails = thumbnails;
      this.state = STATE_VALID;
   }

   /**
    * Get the name of this attachment object
    *
    * @return The name of the attachment object
    */
   public String getName() {
      return this.name;
   }

   /**
    * Set a new name for this attachment object
    *
    * @param name The new name for the attachment object
    */
   public void setName(String name) {
      this.name = name;
   }

   /**
    * Get the type of this attachment object
    *
    * @return The type of the attachment object
    */
   public String getType() {
      return this.type;
   }

   /**
    * Set a new type for this attachment object
    *
    * @param type The new type for the attachment object
    */
   public void setType(String type) {
      this.type = type;
   }

   /**
    * Get the state of this attachment object
    *
    * @return The state of the attachment object
    */
   public int getState() {
      return this.state;
   }

   /**
    * Set a new state for this attachment object
    *
    * @param state The new state for the attachment object
    */
   public void setState(int state) {
      this.state = state;
   }

   /**
    * Get the preview image of this attachment object
    *
    * @return The preview image of the attachment object
    */
   public Bitmap getThumbnails() {
      return this.thumbnails;
   }

   /**
    * Set a new preview image for this attachment object
    *
    * @param thumbnails The new preview image for the attachment object
    */
   public void setThumbnails(Bitmap thumbnails) {
      this.thumbnails = thumbnails;
   }

   /**
    * Check if the attachment object is empty
    *
    * @return True if the attachment object is empty, false otherwise
    */
   public boolean isEmpty() {
      return this.name.equals("") &&
              this.type.equals("") &&
              this.thumbnails == null;
   }

   @Override
   public String toString() {
      return "AttachmentItem{" +
              ", name='" + name + '\'' +
              ", type='" + type + '\'' +
              ", thumbnails=" + thumbnails +
              '}';
   }

   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof AttachmentItem)) return false;

      AttachmentItem that = (AttachmentItem) o;

      if (!name.equals(that.name)) return false;
      if (!thumbnails.equals(that.thumbnails)) return false;
      if (!type.equals(that.type)) return false;

      return true;
   }

   @Override
   public int hashCode() {
      int result = name.hashCode();
      result = 31 * result + type.hashCode();
      result = 31 * result + thumbnails.hashCode();
      return result;
   }

    /* ********************************************************************************************
     *                                        PARCELABLE METHODS                                  *
     **********************************************************************************************/

   public static final Parcelable.Creator<AttachmentItem> CREATOR
           = new Parcelable.Creator<AttachmentItem>() {
      public AttachmentItem createFromParcel(Parcel in) {
         return new AttachmentItem(in);
      }

      public AttachmentItem[] newArray(int size) {
         return new AttachmentItem[size];
      }
   };

   /**
    * Describe the content of the parcelable
    *
    * @return Always zero
    */
   public int describeContents() {
      return 0;
   }

   /**
    * Save an Attachment in the parcelable
    *
    * @param out   The output parcelable
    * @param flags Flags used to save
    */
   public void writeToParcel(Parcel out, int flags) {
      out.writeString(name);
      out.writeString(type);
      out.writeParcelable(thumbnails, flags);
   }

   /**
    * Recreate an Attachment from the parcelable
    *
    * @param in The parcelable where read
    */
   private AttachmentItem(Parcel in) {
      this.name = in.readString();
      this.type = in.readString();
      this.thumbnails = in.readParcelable(Bitmap.class.getClassLoader());
   }

}
